﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using ElectronicCommerce.Framework.ExceptionManagement;
using ElectronicCommerce.Framework.Web.Configuration;
using ElectronicCommerce.Framework.Web.Configuration.StringResource.HtmlBlock;
using ElectronicCommerce.Framework.Web.ResourceManagement;
using ElectronicCommerce.Framework.Web.StateManagement;

namespace ElectronicCommerce.Framework.Web.UI
{
    public abstract class PageBase : Page
    {
        private const string HTTPCONTEXT_NAME = "PageBase";

        public PageBase()
        {
            ECContext.Current.Context.Items[HTTPCONTEXT_NAME] = this;
        }

        public static PageBase Current
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return null;
                }
                return HttpContext.Current.Items[HTTPCONTEXT_NAME] as PageBase;
            }
        }

        private PageHead _pageHead = null;
        public PageHead PageHead
        {
            get
            {
                if (_pageHead == null)
                {
                    _pageHead = new PageHead();
                }
                return _pageHead;
            }
        }

        private PageFoot _pageFoot = null;
        public PageFoot PageFoot
        {
            get
            {
                if (_pageFoot == null)
                {
                    _pageFoot = new PageFoot();
                }
                return _pageFoot;
            }
        }

        private ResourceBuilder _resourceBuilder;
        public ResourceBuilder Resource
        {
            get
            {
                if (_resourceBuilder == null)
                {
                    _resourceBuilder = new ResourceBuilder();
                }
                return _resourceBuilder;
            }
        }

        private UrlBuilder _urlBuilder;
        public UrlBuilder Url
        {
            get
            {
                if (_urlBuilder == null)
                {
                    _urlBuilder = new UrlBuilder();
                }
                return _urlBuilder;
            }
        }

        public IStateProvider State
        {
            get { return StateProvider.Current; }
        }

        private ExManager _exManager;
        public ExManager ExceptionManager
        {
            get
            {
                if (_exManager == null)
                {
                    _exManager = new ExManager();
                }
                return _exManager;
            }
        }


        public string BuildText(string stringResource)
        {
            return ConfigHelper.StringResourceConfig.GetText(stringResource);
        }

        /// <summary>
        /// Get a html block string by the alias
        /// </summary>
        /// <param name="alias"></param>
        /// <returns>The block html or empty string if there is no such alias in config files</returns>
        public string BuildHtml(string alias)
        {
            string html = string.Empty;

            HtmlBlockItem htmlBlock = ConfigHelper.HtmlBlockConfig.GetHtmlBlock(alias);
            if (htmlBlock != null)
            {
                html = htmlBlock.Content;
            }

            return html;
        }

        /// <summary>
        /// Build image 
        /// </summary>
        /// <param name="mediaName"></param>
        /// <returns></returns>
        public string BuildMedia(string mediaName)
        {
            return Resource.BuildMedia(mediaName);
        }

        /// <summary>
        /// Build error message
        /// </summary>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public virtual string BuildErrorMessage(string errorCode)
        {
            return ElectronicCommerce.Framework.Configuration.ConfigHelper.ErrorInfoConfig.ErrorInfoList[errorCode].String;
        }

        /// <summary>
        /// Build Error Title
        /// </summary>
        /// <param name="errorCode"></param>
        /// <returns></returns>
        public virtual string BuildErrorTitle(string errorCode)
        {
            return ElectronicCommerce.Framework.Configuration.ConfigHelper.ErrorInfoConfig.ErrorInfoList[errorCode].Title;
        }
        #region [  Helpers  ]

        public void SetNoCache()
        {
            Response.Cache.SetExpires(DateTime.Now.AddHours(-1));
            Response.CacheControl = "no-cache";
            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        #endregion
    }
}
